import {MangaDBTable} from "./MangaDBTable";
import moment from "moment";

export namespace TableBookList {
    export interface Data extends MangaDBTable.BaseData {
        id: number;
        name: string;
        author: string;
        cover: string;
        last_update_time: number;
        update_frequency: string;
        last_read_article: number;
        update_refer_url: string;
        update_method: string;
        max_article_index: number;
    }
}

export class TableBookList extends MangaDBTable<TableBookList.Data> {
    public static getInstance() {
        return new TableBookList();
    }
    protected defaultData: Partial<TableBookList.Data> = {
        last_read_article: 0,
        update_method: "",
        update_frequency: "week",
        last_update_time: moment().startOf("day").toDate().getTime(),
        max_article_index: 0,
    };
    constructor() {
        super("book_list");
    }
    public saveData(
        data: Partial<TableBookList.Data>,
        mainKey: keyof TableBookList.Data | Array<keyof TableBookList.Data> = "id"
    ): Promise<void> {
        return super.saveData(this.mixedDefaultData(data), mainKey);
    }
    public install() {
        const sql = `CREATE TABLE IF NOT EXISTS book_list (
            id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
            name VARCHAR(240) NOT NULL,
            author VARCHAR(60) NOT NULL,
            cover TEXT NOT NULL,
            last_update_time INTEGER,
            update_frequency TEXT,
            last_read_article INTEGER,
            update_refer_url TEXT,
            update_method TEXT,
            max_article_index INTEGER DEFAULT 0
        );`;
        return this.runBySql(sql);
    }
}
